﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;
    using System.Collections;
    using System.Xml;

    public partial class frmCapNhatDmNhaCungCap : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmNhaCungCapEvent;
        int State = 0;
        DmNhaCungCapEntity dmNhaCungCapEntity = null;

        DmNhaCungCapBusiness dmNhaCungCapBusiness = new DmNhaCungCapBusiness();
        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();

        public frmCapNhatDmNhaCungCap(int state)
        {
            InitializeComponent();
            this.State = state;
        }

        public frmCapNhatDmNhaCungCap(int state, DmNhaCungCapEntity obj)
        {
            InitializeComponent();

            this.State = state;
            dmNhaCungCapEntity = obj;

            txtMaNhaCungCap.Text = dmNhaCungCapEntity.MaNhaCungCap;
            txtTenNhaCungCap.Text = dmNhaCungCapEntity.TenNhaCungCap;
            txtDiaChi.Text = dmNhaCungCapEntity.DiaChi;
            txtDienThoai.Text = dmNhaCungCapEntity.DienThoai;
            txtFax.Text = dmNhaCungCapEntity.Fax;
            txtEmail.Text = dmNhaCungCapEntity.Email;
            txtWebsite.Text = dmNhaCungCapEntity.Website;
            txtMoTa.Text = dmNhaCungCapEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmNhaCungCapEntity.TrangThaiSuDung;
            txtMaNhaCungCap.Enabled = false;
            txtTenNhaCungCap.Focus();
        }

        private void frmCapnhatdmnhacungcap_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaNhaCungCap, "Mã nhà cung cấp") == false) return;
            if (Validation.ValidEmail(txtEmail, "Email") == false) return;
            
            dmNhaCungCapEntity = new DmNhaCungCapEntity()
            {
                MaNhaCungCap = txtMaNhaCungCap.Text,
                TenNhaCungCap = txtTenNhaCungCap.Text.Trim(),
                DiaChi = txtDiaChi.Text.Trim(),
                DienThoai = txtDienThoai.Text.Trim(),
                Fax = txtFax.Text.Trim(),
                Email = txtEmail.Text.Trim(),
                Website = txtWebsite.Text.Trim(),
                MoTa = txtMoTa.Text.Trim(),
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked
            };

            if (this.State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmNhaCungCapBusiness.GetDmNhaCungCap(txtMaNhaCungCap.Text.Trim()) != null)
                    if (Validation.ValidPrimaryKey(txtMaNhaCungCap, "Mã nhà cung cấp") == false) return;

                dmNhaCungCapBusiness.InsertDmNhaCungCap(dmNhaCungCapEntity);
                Validation.ClearControl(this);
                txtMaNhaCungCap.Focus();
            }
            else
            {
                dmNhaCungCapBusiness.EditDmNhaCungCap(dmNhaCungCapEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmNhaCungCapEvent != null)
                dmNhaCungCapEvent(sender, e);
        }
    }
}